import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import store from './store'
//使用 iView
import ViewUI from 'view-design';
import {Message} from 'view-design';
import 'view-design/dist/styles/iview.css';
//使用 ElementUI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//兄弟组件传值
import Bus from "@/tools/bus";
//加密
import Base64 from "js-base64";
import Md5 from "js-md5";

//#################### 使用各类插件 start ###################
//使用 iView
Vue.use(ViewUI);
//使用 ElementUI
Vue.use(ElementUI);
//生产环境提示关闭
Vue.config.productionTip = false
//原型链挂载 axios
Vue.prototype.axios = axios
//Vuex
Vue.prototype.Store = store;
//组件传值
Vue.prototype.Bus = Bus;

//引入加密
Vue.prototype.Base64 = Base64;
Vue.prototype.Md5 = Md5;
//自定义全局提示配置
Vue.prototype.$Message.config({
    top: 100,
    duration: 6
})
//#################### 使用各类插件 end ####################


//#################### 基础配置  start ######################################
axios.defaults.baseURL = "http://localhost:18080/admin"
// axios.defaults.baseURL = "http://localhost:10200"
// 解决 options 预请求
axios.defaults.headers = {
    "Content-Type": "application/json;charset=utf-8"
};
//#################### 基础配置  end #########################################


//#################### 请求响应拦截器 ####################
axios.interceptors.request.use((req) => {
    let isLogin = window.sessionStorage.getItem("isLogin");
    const sessionToken = window.sessionStorage.getItem("token");
    if (isLogin && sessionToken != null) {
        req.headers.Authorization = sessionToken
    }
    return req;
})

axios.interceptors.response.use(
    (resp) => {
        let data = resp.data;
        if (data.code == 500) {
            clog
            Message.error({
                background: true,
                duration: 4,
                content: '系统繁忙......'
            });
            Message.info({
                background: true,
                duration: 8,
                content: '请联系管理员 152 5192 2665'
            });
        }
        return data;
    },
    (error) => {
        let resp = error.response;
        if (resp.status == 500) {
            Message.error({
                background: true,
                duration: 4,
                content: '系统繁忙...'
            });
            Message.info({
                background: true,
                duration: 8,
                content: '请联系管理员 152 5192 2665'
            });
        }
        return Promise.reject(error);
    })
//#################### 请求响应拦截器 ####################


//****************  数组扩展  ********************
//对原生数组扩展, 在第 i 个前面插入 item
Array.prototype.insert = function (index, item) {
    this.splice(index - 1, 0, item);
}
//删除数组第 i 个
Array.prototype.delete = function (index) {
    this.splice(index, 1);
}

//==================== Vue实例 ====================
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
